****
** Replication File for:
**
** The Partisan Contours of Rights and Liberties
**
** Miles Armaly and Adam Enders
**
** Political Behavior
**
** This code replicates the trasntivity analysis ONLY
** The remainder of the code can be located in a separate do file
****

set more off

import delimited "Raw Data, March 2022.csv"

********************************************************************************

****
** Recode and create variables
****

* Attention checks
gen pass1 = 0
replace pass1 = 1 if q23 == 3

gen pass2 = 0
replace pass2 = 1 if q28_5 == 23

gen pass3 = 0
replace pass3 = 1 if q34_5 == 11

gen pass4 = 0
replace pass4 = 1 if q37 == 1

keep if pass1 == 1 & pass2 == 1 & pass3 == 1 & pass4 == 1


* Time spent completing survey
gen short = 0
sum duration, d
* under 5% removed; < 6 minutes on 15 minute survey
replace short = 1 if duration < 353
drop if short == 1


* Generate unique ID
gene id = _n

drop if q13_1 == .

********************************************************************************

****
** Transitivity and consistency analysus
****

** Dominance for "most" and "least" questions

* Free press, most
gen pressprivM1 = (q13_1==1)
gen pressprivM2 = (q15_1==1)
gen pressprivM3 = (q19_2==1)

gen pressarmsM1 = (q13_1==1)
gen pressarmsM2 = (q14_1==1)
gen pressarmsM3 = (q20_2==1)

gen pressreligM1 = (q14_1==1)
gen pressreligM2 = (q15_1==1)
gen pressreligM3 = (q22_3==1)

gen pressspeechM1 = (q19_2==1)
gen pressspeechM2 = (q20_2==1)
gen pressspeechM3 = (q22_3==1)

* Privacy, most
gen privarmsM1 = (q13_2==1)
gen privarmsM2 = (q18_3==1)
gen privarmsM3 = (q21_2==1)

gen privreligM1 = (q15_3==1)
gen privreligM2 = (q17_3==1)
gen privreligM3 = (q21_2==1)

gen privspeechM1 = (q17_3==1)
gen privspeechM2 = (q18_3==1)
gen privspeechM3 = (q19_3==1)

* Bear arms, most
gen armsreligM1 = (q14_3==1)
gen armsreligM2 = (q16_3==1)
gen armsreligM3 = (q21_3==1)

gen armsspeechM1 = (q16_3==1)
gen armsspeechM2 = (q18_2==1)
gen armsspeechM3 = (q20_3==1)

* Religion, most
gen religspeechM1 = (q16_2==1)
gen religspeechM2 = (q17_2==1)
gen religspeechM3 = (q22_2==1)



* Free press, least
gen pressprivL1 = (q13_2==3)
gen pressprivL2 = (q15_3==3)
gen pressprivL3 = (q19_3==3)

gen pressarmsL1 = (q13_3==3)
gen pressarmsL2 = (q14_3==3)
gen pressarmsL3 = (q20_3==3)

gen pressreligL1 = (q14_2==3)
gen pressreligL2 = (q15_2==3)
gen pressreligL3 = (q22_2==3)

gen pressspeechL1 = (q19_1==3)
gen pressspeechL2 = (q20_1==3)
gen pressspeechL3 = (q22_1==3)

* Privacy, least
gen privarmsL1 = (q13_3==3)
gen privarmsL2 = (q18_2==3)
gen privarmsL3 = (q21_3==3)

gen privreligL1 = (q15_2==3)
gen privreligL2 = (q17_2==3)
gen privreligL3 = (q21_1==3)

gen privspeechL1 = (q17_1==3)
gen privspeechL2 = (q18_1==3)
gen privspeechL3 = (q19_1==3)

* Bear arms, least
gen armsreligL1 = (q14_2==3)
gen armsreligL2 = (q16_2==3)
gen armsreligL3 = (q21_1==3)

gen armsspeechL1 = (q16_1==3)
gen armsspeechL2 = (q18_1==3)
gen armsspeechL3 = (q20_1==3)

* Religion, least
gen religspeechL1 = (q16_1==3)
gen religspeechL2 = (q17_1==3)
gen religspeechL3 = (q22_1==3)


** Add "most" and "least" together

* Press
gen presspriv1 = pressprivM1 + pressprivL1
gen presspriv2 = pressprivM2 + pressprivL2
gen presspriv3 = pressprivM3 + pressprivL3

gen pressarms1 = pressarmsM1 + pressarmsL1
gen pressarms2 = pressarmsM2 + pressarmsL2
gen pressarms3 = pressarmsM3 + pressarmsL3

gen pressrelig1 = pressreligM1 + pressreligL1
gen pressrelig2 = pressreligM2 + pressreligL2
gen pressrelig3 = pressreligM3 + pressreligL3

gen pressspeech1 = pressspeechM1 + pressspeechL1
gen pressspeech2 = pressspeechM2 + pressspeechL2
gen pressspeech3 = pressspeechM3 + pressspeechL3

* Privacy
gen privarms1 = privarmsM1 + privarmsL1
gen privarms2 = privarmsM2 + privarmsL2
gen privarms3 = privarmsM3 + privarmsL3

gen privrelig1 = privreligM1 + privreligL1
gen privrelig2 = privreligM2 + privreligL2
gen privrelig3 = privreligM3 + privreligL3

gen privspeech1 = privspeechM1 + privspeechL1
gen privspeech2 = privspeechM2 + privspeechL2
gen privspeech3 = privspeechM3 + privspeechL3

* Bear arms
gen armsrelig1 = armsreligM1 + armsreligL1
gen armsrelig2 = armsreligM2 + armsreligL2
gen armsrelig3 = armsreligM3 + armsreligL3

gen armsspeech1 = armsspeechM1 + armsspeechL1
gen armsspeech2 = armsspeechM2 + armsspeechL2
gen armsspeech3 = armsspeechM3 + armsspeechL3

* Religion
gen religspeech1 = religspeechM1 + religspeechL1
gen religspeech2 = religspeechM2 + religspeechL2
gen religspeech3 = religspeechM3 + religspeechL3


** Recalculate pairwise choices

gen presspriv = (presspriv1>=1) + (presspriv2>=1) + (presspriv3>=1)
gen pressarms = (pressarms1>=1) + (pressarms2>=1) + (pressarms3>=1)
gen pressrelig = (pressrelig1>=1) + (pressrelig2>=1) + (pressrelig3>=1)
gen pressspeech = (pressspeech1>=1) + (pressspeech2>=1) + (pressspeech3>=1)

gen privpress = 3-presspriv
gen privarms = (privarms1>=1) + (privarms2>=1) + (privarms3>=1)
gen privrelig = (privrelig1>=1) + (privrelig2>=1) + (privrelig3>=1)
gen privspeech = (privspeech1>=1) + (privspeech2>=1) + (privspeech3>=1)

gen armspress = 3-pressarms
gen armspriv = 3-privarms
gen armsrelig = (armsrelig1>=1) + (armsrelig2>=1) + (armsrelig3>=1)
gen armsspeech = (armsspeech1>=1) + (armsspeech2>=1) + (armsspeech3>=1)

gen religpress = 3-pressrelig
gen religpriv = 3-privrelig
gen religarms = 3-armsrelig
gen religspeech = (religspeech1>=1) + (religspeech2>=1) + (religspeech3>=1)

gen speechpress = 3-pressspeech
gen speechpriv = 3-privspeech
gen speecharms = 3-armsspeech
gen speechrelig = 3-religspeech


** Level of consistency for each value (range 0-12)

gen pressconl = presspriv + pressarms + pressrelig + pressspeech
gen privconl = privpress + privarms + privrelig + privspeech
gen armsconl = armspress + armspriv + armsrelig + armsspeech
gen religconl = religpress + religpriv + religarms + religspeech
gen speechconl = speechpress + speechpriv + speecharms + speechrelig


** Construct ranking determining dominant choice

* Press
gen press = (presspriv>=2) + (pressarms>=2) + (pressrelig>=2) + (pressspeech>=2)

* Privacy
gen privacy = (privpress>=2) + (privarms>=2) + (privrelig>=2) + (privspeech>=2)

* Bear arms
gen arms = (armspress>=2) + (armspriv>=2) + (armsrelig>=2) + (armsspeech>=2)

* Religion
gen religion = (religpress>=2) + (religpriv>=2) + (religarms>=2) + (religspeech>=2)

* Speech
gen speech = (speechpress>=2) + (speechpriv>=2) + (speecharms>=2) + (speechrelig>=2)


** Create intransitivity dummy

gen intran = 0
replace intran = 1 if press==privacy
replace intran = 1 if press==arms
replace intran = 1 if press==religion
replace intran = 1 if press==speech
replace intran = 1 if privacy==arms
replace intran = 1 if privacy==religion 
replace intran = 1 if privacy==speech 
replace intran = 1 if arms==religion
replace intran = 1 if arms==speech
replace intran = 1 if religion==speech

sum intran
di 1-.1730245


*** Intransitivity by partisanship
gen pid = .
replace pid = 1 if q6 == 1
replace pid = 2 if q6 == 2
replace pid = 3 if q7 == 2
replace pid = 4 if q7 == 3 | q7 == 4
replace pid = 5 if q7 == 1
replace pid = 6 if q5 == 2
replace pid = 7 if q5 == 1

gen rep = .
replace rep = 1 if pid > 4 & pid < .
replace rep = 0 if pid < 5

sum intran if rep == 0
di 1-.196162

sum intran if rep == 1
di 1-.1338583


*** Construct ranking determining perfect consistency choice

** How many times chosen every time most important

* Press
gen PCpress3 = (presspriv==3) + (pressarms==3) + (pressrelig==3) + (pressspeech==3)

* Privacy
gen PCpriv3 = (privpress==3) + (privarms==3) + (privrelig==3) + (privspeech==3)

* Bear arms
gen PCarms3 = (armspress==3) + (armspriv==3) + (armsrelig==3) + (armsspeech==3)

* Religion
gen PCrelig3 = (religpress==3) + (religpriv==3) + (religarms==3) + (religspeech==3)

* Speech
gen PCspeech3 = (speechpress==3) + (speechpriv==3) + (speecharms==3) + (speechrelig==3)



** How many times chosen every time least important

* Press
gen PCpress0 = (presspriv==0) + (pressarms==0) + (pressrelig==0) + (pressspeech==0)

* Privacy
gen PCpriv0 = (privpress==0) + (privarms==0) + (privrelig==0) + (privspeech==0)

* Bear arms
gen PCarms0 = (armspress==0) + (armspriv==0) + (armsrelig==0) + (armsspeech==0)

* Religion
gen PCrelig0 = (religpress==0) + (religpriv==0) + (religarms==0) + (religspeech==0)

* Speech
gen PCspeech0 = (speechpress==0) + (speechpriv==0) + (speecharms==0) + (speechrelig==0)



** Now combine the two for the new Perfect Consistency Measure

* Take the PC3 score and match that with the rank score

gen PC3press = 1 if PCpress3 == press
recode PC3press (.=0)

gen PC3priv = 1 if PCpriv3 == privacy
recode PC3priv (.=0)

gen PC3arms = 1 if PCarms3 == arms
recode PC3arms (.=0)

gen PC3relig = 1 if PCrelig3 == religion
recode PC3relig (.=0)

gen PC3speech = 1 if PCspeech3 == speech
recode PC3speech (.=0)



** Now that we have these scores, we simply add them together

gen presspcon = 1 if (PCpress3+PCpress0)==4
recode presspcon (.=0)

gen privpcon = 1 if (PCpriv3+PCpriv0)==4
recode privpcon (.=0)

gen armspcon = 1 if (PCarms3+PCarms0)==4
recode armspcon (.=0)

gen religpcon = 1 if (PCrelig3+PCrelig0)==4
recode religpcon (.=0)

gen speechpcon = 1 if (PCspeech3+PCspeech0)==4
recode speechpcon (.=0)



** Consistency score by rank position

gen con1p = 1 if (presspcon==1 & press==4) | (privpcon==1 & privacy==4) | ///
	(armspcon==1 & arms==4) | (religpcon==1 & religion==4) | (speechpcon==1 & speech==4)
recode con1p (.=0)

gen con2p = 1 if (presspcon==1 & press==3) | (privpcon==1 & privacy==3) | ///
	(armspcon==1 & arms==3) | (religpcon==1 & religion==3) | (speechpcon==1 & speech==3)
recode con2p (.=0)

gen con3p = 1 if (presspcon==1 & press==2) | (privpcon==1 & privacy==2) | ///
	(armspcon==1 & arms==2) | (religpcon==1 & religion==2) | (speechpcon==1 & speech==2)
recode con3p (.=0)

gen con4p = 1 if (presspcon==1 & press==1) | (privpcon==1 & privacy==1) | ///
	(armspcon==1 & arms==1) | (religpcon==1 & religion==1) | (speechpcon==1 & speech==1)
recode con4p (.=0)

gen con5p = 1 if (presspcon==1 & press==0) | (privpcon==1 & privacy==0) | ///
	(armspcon==1 & arms==0) | (religpcon==1 & religion==0) | (speechpcon==1 & speech==0)
recode con5p (.=0)


graph bar (mean) con1p (mean) con2p (mean) con3p (mean) con4p (mean) con5p, ///
	ytitle(Proportion of Respondents) ///
	b1title(Rank Position) ///
	bar(1, color(gray) lcolor(black)) ///
	bar(2, color(gray) lcolor(black)) ///
	bar(3, color(gray) lcolor(black)) ///
	bar(4, color(gray) lcolor(black)) ///
	bar(5, color(gray) lcolor(black)) ///
	legend(off) ///
	scheme(s1manual) 


** Recode ranks from 4-0 to 1-5

recode press (4=1) (3=2) (2=3) (1=4) (0=5)
recode privacy (4=1) (3=2) (2=3) (1=4) (0=5)
recode arms (4=1) (3=2) (2=3) (1=4) (0=5)
recode religion (4=1) (3=2) (2=3) (1=4) (0=5)
recode speech (4=1) (3=2) (2=3) (1=4) (0=5)


** Generate rank variables with values as cell entries

* 1=press, 2=priv, 3=arms, 4=relig, 5=speech

gen rank1 = 1 if press==1
replace rank1 = 2 if privacy==1
replace rank1 = 3 if arms==1
replace rank1 = 4 if religion==1
replace rank1 = 5 if speech==1

gen rank2 = 1 if press== 2
replace rank2 = 2 if privacy==2
replace rank2 = 3 if arms==2
replace rank2 = 4 if religion==2
replace rank2 = 5 if speech==2
 
gen rank3 = 1 if press==3
replace rank3 = 2 if privacy==3
replace rank3 = 3 if arms==3
replace rank3 = 4 if religion==3
replace rank3 = 5 if speech==3

gen rank4 = 1 if press==4
replace rank4 = 2 if privacy==4
replace rank4 = 3 if arms==4
replace rank4 = 4 if religion==4
replace rank4 = 5 if speech==4

gen rank5 = 1 if press==5
replace rank5 = 2 if privacy==5
replace rank5 = 3 if arms==5
replace rank5 = 4 if religion==5
replace rank5 = 5 if speech==5


** Save data to append to rankings
replace religion = . if q13_1 == .
replace speech = . if q13_1 == .
replace press = . if q13_1 == .
replace arms = . if q13_1 == .
replace privacy = . if q13_1 == .

keep id press privacy arms religion speech

saveold "/Users/adamenders/Dropbox/Rights and PID/Data and Code/Survey 1, March 2022/Triad Rankings.dta", version(12) replace




